// sysvals.cpp
// Customizer
// Protea Customizer / Business Logic for managing system values
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//////////////////////////////////////////////////////////////////////////////
//
// SYSTEM_VALUES Transactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 2
//@ CHA Change System Value
//@ COD ChangeSystemValue
//@ NUM 190
//@ CRI FIELD_NAME
//@ CRI APPL_NBR
//@ TAB System_Values Write
//
//@ GEN ChgSysValue will review/change system values
//
TRANSACTION_CODE( ChangeSystemValue ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* OrigBuf = NULL;
	bool ReviewOnly = false;

	//@ GRS Start the transaction
	START_TXN( ChangeSystemValue );

		//@ Create format descriptor buffer
		TransBuf = App->CreateBuffer( FMT_SYSTEM_VALUES, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		ReviewOnly = txnvars.buffer->IsBlank( 2, FLD_APPL_NBR, FLD_FIELD_NAME );
		START_DB_TXN();
			txnvars.Errors->Check();
			OrigBuf = TransBuf->Copy();
			SystemValueVerify( TransBuf, ReviewOnly ? VERIFY_REVIEW : VERIFY_CHANGE, &txnvars, OrigBuf );
			txnvars.Errors->Check();

			if (!ReviewOnly) {
				OrigBuf->MoveField( TransBuf, FLD_FIELD_NUMBER );

				//@ SUB SystemValueChange change field record
				SystemValueChange( OrigBuf, TransBuf, &txnvars );
			};

			LOG_TRANSACTION();

		END_DB_TXN();

		//@ Send back result buffer
		if (ReviewOnly)
			txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
		else
			txnvars.buffer->MoveCorresponding( OrigBuf, txnvars.Errors  );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 2, &TransBuf, &OrigBuf );
	END_TXN();
	//@ GRE End the transaction
};



//
//@ CHA GetSysVals
//@ COD GetSysVals
//@ NUM 190
//@ CRI APPL_NBR
//@ TAB SYSTEM_VALUES Read
//
//@ GEN GetSysVals returns all system values
//
TRANSACTION_CODE( GetSysVals ) {
	XCBuffer* TransBuf = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( GetSysVals );
		XCSQLGen* query = txnvars.query;

		//@ Create transaction defn buffer
		TransBuf = App->CreateBuffer( FMT_SYSTEM_VALUES, false );

		//@ Move screen info to transaction buffer
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			int child = txnvars.buffer->GetChildFormat();
			if (child) {
				detail = App->CreateBuffer( child, true );

				// Formulate select statement
				query->MakeSELECT( TransBuf );

				// If Appl_nbr = 0, select all records
				XCCompare comptemp;
				XCField* temp = TransBuf->GetField( FLD_APPL_NBR );
				if (temp->Compare(0) != 0)
					query->AddWHERE( TransBuf->GetField( FLD_APPL_NBR ), &comptemp );

				// Execute the query
				stmt = txnvars.dbtxn->ExecuteSQL( query );

				while (!stmt->Eof()) {
					detail->Clear();
					detail->MoveCorresponding( TransBuf, txnvars.Errors, true );
					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					stmt->MoveNext();
				};
			};
		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 3, &TransBuf, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};
